package transitApiHttp.apiOther.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.concurrent.Callable;

@Controller
public class TransferApi {

    private RestTemplate restTemplate;
    @Autowired
    public void setRestTemplate(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    /**
     * 一。get请求返回String类型
     * 注意点：对方那边只要有返回值就行
     *
     * @param request
     * @return
     * @throws InterruptedException
     */
    @RequestMapping("/getString")
    @ResponseBody
    public Callable<String> getString(HttpServletRequest request) {

        System.out.println("主线程开始了");
        Callable<String> result = new Callable<String>() {
            @Override
            public String call() throws Exception {
                System.out.println("副线程开始了");
                String response = restTemplate.getForObject(request.getParameter("url"), String.class);
                System.out.println("副线程返回了");
                return response;
            }
        };
        System.out.println("主线程返回");
        return result;
    }

    /**
     * 二。get请求返回HashMap类型
     * 注意点：对方那边必须返回json格式的键值对，如：{"k":"值","k2":"值"}
     * 否则会报错
     *
     * @param request
     * @return
     * @throws InterruptedException
     */
    @RequestMapping("/getHashMap")
    @ResponseBody
    public Callable<HashMap> getHashMap(HttpServletRequest request) {
        System.out.println("request：" + request.getParameter("url"));
        System.out.println("主线程开始了");
        Callable<HashMap> result = new Callable<HashMap>() {
            @Override
            public HashMap call() throws Exception {
                System.out.println("副线程开始了");
                HashMap<String, Object> response = restTemplate.getForObject(request.getParameter("url"), HashMap.class);
                System.out.println("副线程返回了");
                return response;
            }
        };
        System.out.println("主线程返回");
        return result;
    }

    /**
     * 三。post请求中转，返回string类型
     * 注意：只要请求的对象有返回值就可以
     *
     * @param request
     * @return
     * @throws InterruptedException
     */
    @PostMapping("/postString")
    @ResponseBody
    public Callable<String> postString(HttpServletRequest request) {
        LinkedMultiValueMap<String, Object> valueMap = new LinkedMultiValueMap<>();//创建参数map
        Enumeration<String> parameterNames = request.getParameterNames();//获取枚举将其存为键名
        while (parameterNames.hasMoreElements()) { //遍历出键名  并把参数值打印出来
            String s = parameterNames.nextElement();//执行一次
            if (!s.equals("url")) { //不把url添加进去 注意：java当中比较字符串是否相等用equals()
                valueMap.add(s, request.getParameter(s));//添加进valueMap一会儿作为参数
                System.out.println("s是：" + s);//名称
                System.out.println(request.getParameter(s));//值
            }
        }
        Callable<String> result = new Callable<String>() {
            @Override
            public String call() throws Exception {
                System.out.println("副线程开始了");
                String s = restTemplate.postForObject(request.getParameter("url"), valueMap, String.class);

                return s;
            }
        };
        System.out.println("主线程返回");
        return result;
    }

    /**
     * 四。post请求返回HashMap类型
     * 这种方式常用。
     * 注意点：对方那边必须返回json格式的键值对，如：{"k":"值","k2":"值"}才能使用此方法
     * 否则会报错
     *
     * @param request
     * @return
     */
    @PostMapping("/postHashMap")
    @ResponseBody
    public Callable<HashMap> postHashMap(HttpServletRequest request) {
        LinkedMultiValueMap<String, Object> valueMap = new LinkedMultiValueMap<>();
        Enumeration<String> parameterNames = request.getParameterNames();
        while (parameterNames.hasMoreElements()) {
            String s = parameterNames.nextElement();
            if (!s.equals("url")) {
                valueMap.add(s, request.getParameter(s));
                System.out.println("s是：" + s);//名称
                System.out.println(request.getParameter(s));//值
            }
        }
        Callable<HashMap> result = new Callable<HashMap>() {
            @Override
            public HashMap call() throws Exception {
                HashMap<String, Object> responseBody = restTemplate.postForObject(request.getParameter("url"), valueMap, HashMap.class);
                System.out.println("打印副线程返回值：" + responseBody);
                return responseBody;
            }
        };
        System.out.println("主线程返回了");
        return result;
    }

    /**
     * post和get的测试接口
     *
     * @param request
     * @return
     */
    @PostMapping("/postTest")
    @ResponseBody
    public String postTest(HttpServletRequest request) {
        System.out.println("跳转请求成功");
        return "{\"k\":\"值\",\"k2\":\"值\"}";
    }

    @GetMapping("/getTest")
    @ResponseBody
    public String getTest(HttpServletRequest request) {
        System.out.println("跳转请求成功");
        return "{\"k\":\"值\",\"k2\":\"值\"}";
    }
}
